package com.zy.explore.sys.application.service;

import com.zy.explore.common.infrastructure.base.mybatis.BaseDao;
import com.zy.explore.common.infrastructure.base.mybatis.BaseServiceImpl;
import com.zy.explore.common.interfaces.vo.PageVO;
import com.zy.explore.sys.domain.mybatis.dao.SysDeptDao;
import com.zy.explore.sys.domain.mybatis.dao.SysUserDao;
import com.zy.explore.sys.domain.mybatis.entity.SysDept;
import com.zy.explore.sys.domain.mybatis.entity.SysUser;
import com.zy.explore.sys.interfaces.frm.DeptMemberPageFrm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统部门表(表sys_dept)服务接口实现类
 *
 * @author ChinaYin  主页:https://gitee.com/zy-explorej2ee 邮箱:zyexplorej2ee@sina.com
 * @date 2021年06月13日 12:45:39
 */
@Slf4j
@Service
public class SysDeptService extends BaseServiceImpl<SysDept, Integer, Object> {

    /**
     * 注入持久层对象
     */
    @Resource
    private SysDeptDao dao;

    @Resource
    private SysUserDao sysUserDao;

    /**
     * 定义抽象方法getDao获取当前实体Mapper对象
     *
     * @return 当前实体Mapper对象
     */
    @Override
    protected BaseDao<SysDept> getDao() {
        return this.dao;
    }

    /**
     * 查询部门成员分页
     *
     * @param frm 部门成员分页请求对象
     * @return {@link PageVO <SysUser>}
     */
    public PageVO<SysUser> getDeptMemberPage(DeptMemberPageFrm frm) {
        Long deptId = frm.getDeptId();
        String keyword = frm.getKeyword();
        String leaveStatus = frm.getLeaveStatus();
        String empStatus = frm.getEmpStatus();
        Integer pageNum = frm.getPageNum();
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        Integer pageSize = frm.getPageSize();
        if (pageSize == null || pageSize <= 0) {
            pageSize = 10;
        }
        long total = sysUserDao.getTotal(deptId, keyword, leaveStatus, empStatus);
        List<SysUser> list = sysUserDao.getDeptMemberList(deptId, keyword, leaveStatus, empStatus, (pageNum - 1) * pageSize , pageSize);
        PageRequest pageRequest = PageRequest.of(pageNum - 1, pageSize);
        Page<SysUser> page = new PageImpl<>(list, pageRequest, total);
        return new PageVO<>(page);
    }

}